using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using Platform.ChooseTextPanel;

namespace Platform.algorithm
{
    class KnowledgeBase_File : KnowledgeBase
    {
        public KnowledgeBase_File()
        {
            getAllKnowledge();
            loadFrequency();
            loadBihua();
        }
        protected void loadFrequency()
        {
            try
            {
                string txt = Util.GetFileTxt(GlobalSetting.GetUnitOrderFile(), Encoding.UTF8);
                txt = txt.Trim() + "\r\n";
                string[] lines = txt.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < lines.Length; ++i)
                {
                    string[] a = lines[i].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    this.BaseFrequency.Add(a[0].Trim(), Int32.Parse(a[1].Trim()));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        protected void loadBihua()
        {
            try
            {
                string txt = Util.GetFileTxt(GlobalSetting.GetBihuaFile(), Encoding.UTF8);
                string[] lines = txt.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < lines.Length; ++i)
                {
                    string[] a = lines[i].Trim().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    this.BaseBihua.Add(a[0].Trim(), Int16.Parse(a[1].Trim()));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        protected override void getAllKnowledge()
        {
            string knowledgeTxt = "";
            string file = GlobalSetting.GetKnowledgeFile();
            try
            {
                string txt = Util.GetFileTxt(file, Encoding.UTF8);
                txt = txt.Trim() + "\r\n";
                txt = Regex.Replace(txt, @"[^a-zA-Z0-9,\.\t\r\n\- ]", "");
                txt = txt.Replace('\t', ' ');
                knowledgeTxt += txt;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            string[] lines = knowledgeTxt.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < lines.Length; i++)
            {
                ArrList.Add(new Element(lines[i]));
            }
            ArrList.Sort(new BaseCodeCompare());
        }
    }
}
